const app = getApp()
const $api = require('../../utils/api.js').API;
Page({
    data: {
        city:'全部',
        moneys:['全部','5k以下','5k~8k','8k~10k','10k以上'],
        moneytext:'薪资范围',
        index:0,
        moneyArr:['面议','5k以下','5k~8k','8k~10k','10k以上'],
        citytext:'区域',
        region: ['天津市', '天津市', '全部'],
        customItem: '全部',

        jobCate:[
            {
                'id':0,
                'name':'全部',
                'addtime':'2020-10-20'
            }
        ],
        jobtext:'岗位类别',
        index1:0,
        list:[],
        page:1,
        imgUrl:app.globalData.imgUrl,
        pageShow:true,
        keyword:''

    },
    onLoad: function(){
        wx.showLoading()
        this.getjobCate();
        this.getList();
    },
    getjobCate:function(){
        var data = {}
        var that = this
        var jobCate = that.data.jobCate
        $api.get('job/list',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                that.setData({
                    jobCate:jobCate.concat( res.data.data)
                })
            }
        }).catch(err => {
            //请求失败
            //console.log('job','请求失败');
        })
    },
    getList:function(){
        if(this.data.city == '全部'){
            var city = '';
        }else{
            var city = this.data.city;
        }
        if(this.data.jobCate[this.data.index1]['name'] == '全部'){
            var job = ''
        }else{
            var job = this.data.jobCate[this.data.index1]['name']
        }
        var data = {
            'city':city,
            'page':this.data.page,
            'money':this.data.index,
            'job':job,
            'keyword':this.data.keyword
        }
        var that = this
        var list = that.data.list
        $api.get('company/list',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                if(res.data.data.last_page <= that.data.page){
                    that.setData({
                        pageShow:false
                    })
                }
                wx.hideLoading()
                if(that.data.page == 1){
                    that.setData({
                        list:res.data.data.data
                    })
                }else{
                    that.setData({
                        list:list.concat(res.data.data.data)
                    })
                }
            }
        })
    },
    jobChange: function(e) {
        var jobCate = this.data.jobCate;
        this.setData({
          index1: e.detail.value,
          jobtext:jobCate[e.detail.value]['name'],
          pageShow:true,
          page:1
        })
        wx.showLoading()
        this.getList();
    },
    moneyChange: function(e) {
        var moneys = this.data.moneys;
        this.setData({
            index: e.detail.value,
            moneytext:moneys[e.detail.value],
            pageShow:true,
            page:1
        })
        wx.showLoading()
        this.getList();
    },
    bindRegionChange: function (e) {
        this.setData({
            pageShow:true,
            page:1
        })
        if(e.detail.value[1] !== '全部'){
            this.setData({
                region:e.detail.value,
                citytext: e.detail.value[1]
            })
        }else{
            this.setData({
                region:e.detail.value,
                citytext: e.detail.value[0]
            })
        }
        wx.showLoading()
        this.getList();
    },
    jump:function(e){
        var id = e.currentTarget.dataset.id
        var uid = e.currentTarget.dataset.uid
        var role = wx.getStorageSync('role');
        if(role == 1){//未注册
            wx.navigateTo({
                url: '/pages/login/index'
            })
            return;
        }
        if(role == 3 || role == 4){//机构和代理商
            wx.showToast({
                title:'暂无权限',
                duration:3000,
                mask:true,
                icon:'none', 
            })
            return;
        }
        wx.navigateTo({
            url: '/pages/company/info?id='+id+'&uid='+uid
        })
    },
    onReachBottom: function(t){
        if(this.data.pageShow){
            //当前不是最后一页可加载
            this.setData({
                page: this.data.page + 1,
            })
            wx.showLoading()
            this.getList();
        }
    },
    onShareAppMessage: function() {
        return {
            title: "机构-天意美业直通车",
            path: "/pages/index/index?puid="+wx.getStorageSync('uid')
        };
    },
    search:function(e){
        var value = e.detail.value
        this.setData({
            keyword:value
        })
        wx.showLoading()
        this.getList();
    },
    slectCity:function(){
        wx.navigateTo({
            url: '/pages/selectCity/index'
        })
    }
    
});